package com.IntelligenceInsight.Myth.mapper;

import com.IntelligenceInsight.Myth.model.Algorithm;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AlgorithmMapper {
    @Select("SELECT * FROM algorithm WHERE owner=#{owner} GROUP BY name ORDER BY id DESC")
    List<Algorithm> AlgorithmListByOwner(@Param("owner") String owner);

    @Select("SELECT * FROM algorithm ORDER BY timeCreate DESC LIMIT #{page}, #{limit}")
    List<Algorithm> AlgorithmListAll(@Param("page") int page, @Param("limit") int limit);

    @Select("SELECT * FROM algorithm ORDER BY id DESC")
    List<Algorithm> AlgorithmListAllforName();

    @Select("SELECT COUNT(*) FROM algorithm")
    int AlgorithmListAllCount();

    @Select("SELECT * FROM algorithm WHERE uuid=#{uuid}")
    Algorithm AlgorithmByUuid(@Param("uuid") String uuid);

    @Select("SELECT * FROM algorithm WHERE name=#{name} ORDER BY id DESC LIMIT 1")
    Algorithm AlgorithmByName(@Param("name") String name);

    @Insert("INSERT INTO algorithm(uuid, name, owner, description, defaultParams) VALUES(#{uuid}, #{name}, #{owner}, #{description}, #{defaultParams})")
    int AlgorithmAdd(@Param("uuid") String uuid, @Param("name") String name, @Param("owner") String owner, @Param("description") String description, @Param("defaultParams") String defaultParams);

    @Insert("INSERT INTO algorithm(uuid, name, owner, lastVersion, description, jarPath, defaultParams) VALUES(#{uuid}, #{name}, " +
            "#{owner}, #{lastVersion}, #{description}, #{jarPath}, #{defaultParams})")
    int AlgorithmClone(@Param("uuid") String uuid, @Param("name") String name, @Param("owner") String owner, @Param("lastVersion") int lastVersion, @Param("description") String description, @Param("jarPath") String jarPath, @Param("defaultParams") String defaultParams);

    @Update("UPDATE algorithm set lastVersion=#{lastVersion}, version=#{version}, jarPath=#{jarPath} WHERE uuid=#{uuid}")
    int AlgorithmUpdate(@Param("uuid") String uuid, @Param("lastVersion") int lastVersion, @Param("version") int version, @Param("jarPath") String jarPath);

    @Delete("DELETE FROM algorithm WHERE name=#{name}")
    int AlgorithmRemoveByName(@Param("name") String name);
}
